$(function () {
    htdy_init();
});

function htdy_init() {
    var options = getHtdyGridOption();
    $('#htdy_grid').datagrid(options);
}

function getHtdyGridOption() {
    var columns = [
        [
            {field: 'mc', title: '合同名称', width: 70},
            {field: 'html', title: '合同html', width: 70},
            {field: 'htlx', title: '合同类型', width: 50},
            {field: 'nbgs_jc', title: '公司名称', width: 50},
            {field: 'bz', title: '备注', width: 50}
        ]
    ];
    var options = {
        url: $.webPath() + "/htdygl/htdy/htdyList",
        toolbar: "#htdy_toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        columns: columns,
        rownumbers: true,
        onClickRow: function (rowIndex, rowData) {
            if (rowData != null && rowData.id != null) {
                $("#htdy_mod").show();
                $("#htdy_del").show();
            }
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                this.html = '<a href="javascript:void(0);" onclick="checkDetail(' + this.id + ')" style="color: red">' + "查看详细" + '</a>';
                if (this.htlx == "xsht") {
                    this.htlx = "销售合同";
                }

                if (this.htlx == "cght") {
                    this.htlx = "采购合同";
                }

            })
            return data;
        }

    };
    return options;
}
// 查看详细html
function checkDetail(htdyid) {
    var href = $.webPath() + "/htdygl/htdy/htdyDetail?detailId=" + htdyid
    showDetailHtmlDialog("合同定义详细html", href);
}

// 查看详细html
function showDetailHtmlDialog(title, href) {
    var win = $.window({
        title: title,
        width: 1000,
        height: 550,
        cache: false,
        url: href,
        modal: true
    });
}

//*************按钮触发事件***********
//双击事件
$("#htdy_grid").datagrid({
    onDblClickCell: function (index, field, value) {
        var record = $("#htdy_grid").datagrid("getSelected");
        var record = $("#htdy_grid").datagrid("getSelected");
        if (record == null) {
            $.messager.alert("提示", "请选择要更新的记录!", "info");
            return;
        }
        var href = $.webPath() + "/htdygl/htdy/htdy_content?htdyId=" + record.id;
        var actionUrl = $.webPath() + "/htdygl/htdy/modifyHtdy?htdyId=" + record.id + "&fileType=";
        if (field == "mc") {
            showHtdyDialog("修改合同定义", href, actionUrl);
        }
    }
});

//新增合同定义
$("#htdy_add").click(function () {
    var href = $.webPath() + "/htdygl/htdy/htdy_content";
    var actionUrl = $.webPath() + "/htdygl/htdy/addHtdy?fileType=";
    showHtdyDialog("新增合同定义", href, actionUrl);
});

//修改合同定义
$("#htdy_mod").click(function () {
    var record = $("#htdy_grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "info");
        return;
    }
    var href = $.webPath() + "/htdygl/htdy/htdy_content?htdyId=" + record.id;
    var actionUrl = $.webPath() + "/htdygl/htdy/modifyHtdy?htdyId=" + record.id + "&fileType=";
    showHtdyDialog("修改合同定义", href, actionUrl);
});

// 删除合同定义
$("#htdy_del").click(function () {
    var record = $("#htdy_grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要删除的记录!", "info");
        return;
    }
    $.messager.confirm("提示", "是否确认删除该合同定义?", function (r) {
        if (r) {
            $.ajax({
                type: "POST",
                url: $.webPath() + "/htdygl/htdy/deleteHtdy",
                dataType: "json",
                data: {htdyId: record.id},
                beforeSend: function () {
                    $.messager.progress();
                },
                success: function (data) {
                    $.messager.progress("close");
                    var icon = "info";
                    if (data.statusCode == 300)
                        $.messager.alert("提示", data.message, "error");
                    if (data.statusCode == 200) {
                        $.messager.alert("提示", data.message, "error");
                        loadHtdy();
                    }

                }
            });
        }
    });
});

//弹出新增和修改的窗口
function showHtdyDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 550,
        height: 350,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '提交',
                handler: function () {
                    $.messager.progress();
                    $("#htdyForm").form('submit', {
                        url: actionUrl + $('#htdyHtml').val().substring($('#htdyHtml').val().lastIndexOf(".") + 1).toLowerCase(),
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $.messager.progress('close');
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $.messager.progress('close');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                loadHtdy();
                                $.messager.show({
                                    title: '提示框',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else {
                                $.messager.alert('提示', json.message, 'error');
                            }
                        }
                    });
                }
            }
        ]
    });
}

//重新加载页面
function loadHtdy() {
    $('#htdy_grid').datagrid('reload', {
    });
}

